'use strict';

/**
 * @ngdoc service
 * @name myAppApp.httpInterceptor
 * @description
 * # httpInterceptor
 * Service in the jgappApp.
 */
angular.module('jgappApp').factory('httpInterceptor', ['$q', '$rootScope', '$location', function($q, $rootScope, $location) {
    console.log('http loader was controlled here');
    return {
        request: function(config) {
            config.timeout = 10000;
            if (config.data) {
                if (!config.data.noloader && !config.noloader) {
                    $rootScope.showLoading = true;
                }
            } else if (!config.noloader) {
                $rootScope.showLoading = true;
            }
            return config;
        },
        'response': function(response) {
            $rootScope.showLoading = false;
            var headers = response.headers();
            if (headers.sessionexpired === "true") {
                console.log(1)
                response.data = {}
                response.data.logout = 1
                    // $location.path("login/login")
            }
            return response || $q.when(response);

        },
        'responseError': function(rejection) {
            $rootScope.showLoading = false;
            navigator.notification && navigator.notification.alert("网络出错,请稍后重试",null,"提示")
            return $q.reject(rejection);
        }
    };
}]);
